<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class VideoDB extends BaseDB
{
    /**
     * 视频
     */

    public $tVideo = 'video';
    public $tZan = 'v_zan';
    public $tPl = 'v_pl';
    public $tPt = 'v_pl_txt';
    public $tFx = 'v_fx';
    public $tFt = 'v_fx_time';
    public $tMc = 'u_mc';
    public $tUpload = 'upload';
    public $tThing = 'thing';
    public $tCate = 'cate';
    public $tFarm = 'farm';

    public function __construct()
    {
    }

    /**
     * 一条记录
     * @param $oper1上拉2下拉，$type1果干2农产品
     */
    public function getOneByOper($wx,$id,$oper,$type)
    {
        $query = DB::table($this->tVideo)->where('del',0)->where('type',$type);
        if (!$id) {
            $models = $query->where('id','>=',$id)->orderBy('id','asc')->get();
        } else if ($oper==1) {
            $models = $query->where('id','>',$id)->orderBy('id','asc')->get();
        } else if ($oper==2) {
            $models = $query->where('id','<',$id)->orderBy('id','desc')->get();
        } else {
            return array('code'=>'-2','msg'=>'参数错误');
        }
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $model = $this->jsonToArr($models[0]);
        //封面
        $cover = $model['dycover'] ? $model['dycover'] : '';
        if (!$cover) {
            $cover = $model['kscover'] ? $model['kscover'] : '';
        }
        if (!$cover) {
            $cover = $model['cover'] ? $model['cover'] : '';
        }
        //视频链接
        $link = $model['dylink'] ? $model['dylink'] : '';
        if (!$link) {
            $link = $model['kslink'] ? $model['kslink'] : '';
        }
        if (!$link) {
            $link = $model['link'] ? $model['link'] : '';
        }
        $queryZan = DB::table($this->tZan)->where('del',0)->where('vid',$model['id']);
        $queryPl = DB::table($this->tPt)->where('del',0)->where('vid',$model['id']);
        $queryFx = DB::table($this->tFt)->where('del',0)->where('vid',$model['id']);
        $queryMc = DB::table($this->tMc)->where('del',0)->where('vid',$model['id']);
        $zan = $queryZan->where('wx',$wx)->count(); $zanTotal = $queryZan->count();
        $pl = $queryPl->where('wx',$wx)->count(); $plTotal = $queryPl->count();
        $fx = $queryFx->where('wx',$wx)->count(); $fxTotal = $queryFx->count();
        $mc = $queryMc->where('wx',$wx)->count(); $mcTotal = $queryFx->count();
        //物品信息
        $things = DB::table($this->tThing)->where('id',$model['thing'])->get();
        $thing = count($things) ? $this->jsonToArr($things[0]) : '';
        $type = $thing ? $thing['utype'] : 0;
        //是否被领取
        $farms = DB::table($this->tFarm)->where('del',0)->where('tree',$model['thing'])
            ->whereIn('status',[1,3])->get();
        $isFarm = count($farms) ? 1 : 0;
        $items = [
            'vid'=>$model['id'], 'vtitle'=>$model['title'], 'vcover'=>$cover, 'vlink'=>$link,
            'zan'=>$zan, 'zanTotal'=>$zanTotal, 'pl'=>$pl, 'plTotal'=>$plTotal,
            'fx'=>$fx, 'fxTotal'=>$fxTotal, 'mc'=>$mc, 'mcTotal'=>$mcTotal,
            'type'=>$type, 'tid'=>$model['thing'], 'isFarm'=>$isFarm,
        ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 一条记录
     */
    public function getOneById($id)
    {
        $models = DB::table($this->tVideo)->where('id',$id)->orderBy('id','desc')->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $model = $this->jsonToArr($models[0]);
        //封面
        $cover = $model['dycover'] ? $model['dycover'] : '';
        if (!$cover) {
            $cover = $model['kscover'] ? $model['kscover'] : '';
        }
        if (!$cover) {
            $cover = $model['cover'] ? $model['cover'] : '';
        }
        //视频链接
        $link = $model['dylink'] ? $model['dylink'] : '';
        if (!$link) {
            $link = $model['kslink'] ? $model['kslink'] : '';
        }
        if (!$link) {
            $link = $model['link'] ? $model['link'] : '';
        }
        $items = [
            'vid'=>$model['id'], 'vtitle'=>$model['title'],
            'vcover'=>$cover, 'vlink'=>$link,
        ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 点赞获取
     */
    public function getZanOne($wx,$vid)
    {
        return DB::table($this->tZan)->where('del',0)->where('wx',$wx)->where('vid',$vid)->first();
    }

    /**
     * 点赞增加
     */
    public function setZanAdd($wx,$vid)
    {
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tZan)->insertGetId(['wx'=>$wx,'vid'=>$vid,'created_at'=>time()]);
            if (!$resDb) { return array('code'=>'-2','msg'=>'点赞失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 点赞移除
     */
    public function setZanDel($wx,$vid)
    {
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tZan)->where('wx',$wx)->where('vid',$vid)->update(['del'=>1,'updated_at'=>time()]);
            if (!$resDb) { return array('code'=>'-2','msg'=>'移除失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 分享增加
     */
    public function setFxAdd($wx,$vid)
    {
        $resOnes = DB::table($this->tFx)->where('del',0)
            ->where('wx',$wx)->where('vid',$vid)->get();
        $resOne = count($resOnes) ? $this->jsonToArr($resOnes[0]) : '';
        $fx = $resOne ? $resOne['id'] : 0;
        //开启事务
        DB::beginTransaction();
        try {
            if (!$fx) {
                $lastId = DB::table($this->tFx)->insertGetId(['wx'=>$wx,'vid'=>$vid,'created_at'=>time()]);
            } else {
                $lastId = $fx;
            }
            $resDb = DB::table($this->tFt)->insertGetId(['wx'=>$wx,'vid'=>$vid,'fx'=>$lastId,'created_at'=>time()]);
            if (!$resDb) { return array('code'=>'-2','msg'=>'分享失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 我的页面-列表
     */
    public function getMyIndex($wx)
    {
        $query = DB::table($this->tUpload)->where('wx',$wx);
        $total = $query->count();
        $models = $query->skip(0)->take(10)->orderBy('id','desc')->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $cover = env('DOMAIN').'/'.$model['cover'];
            $link = env('DOMAIN').'/'.$model['link'];
            $addTime = date('Y.m.d',$model['addTime']);
            $items[] = [
                'id'=>$model['id'], 'cover'=>$cover, 'link'=>$link,
                'vw'=>$model['vw'], 'vh'=>$model['vh'], 'delay'=>$model['delay'],
                'del'=>$model['del'], 'addTime'=>$addTime,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    /**
     * 我的页面-喜欢列表
     */
    public function getMyLikeIndex($wx)
    {
        $query = DB::table($this->tZan)->where('del',0)->where('wx',$wx);
        $total = $query->count();
        $models = $query->skip(0)->take(10)->orderBy('id','desc')->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $addTime = date('Y.m.d',$model['created_at']);
            $cover = ''; $link = '';
            $resOne = $this->getOneById($model['vid']);
            if ($resOne['code']==0) {
                $cover = $resOne['data']['vcover'];
                $link =$resOne['data']['vlink'];
            }
            $items[] = [
                'vid'=>$model['vid'], 'cover'=>$cover, 'link'=>$link, 'addTime'=>$addTime,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    /**
     * 我的页面-评论列表
     */
    public function getMyPlIndex($wx)
    {
        $query = DB::table($this->tPl)->where('del',0)->where('wx',$wx);
        $total = $query->count();
        $models = $query->skip(0)->take(10)->orderBy('id','desc')->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $addTime = date('Y.m.d',$model['created_at']);
            $cover = ''; $link = '';
            $resOne = $this->getOneById($model['vid']);
            if ($resOne['code']==0) {
                $cover = $resOne['data']['vcover'];
                $link =$resOne['data']['vlink'];
            }
            $txtQuery = DB::table($this->tPt)->where('del',0)->where('vid',$model['vid']);
            $vtotal = $txtQuery->count();
            $utotal = $txtQuery->where('wx',$wx)->count();
            $items[] = [
                'vid'=>$model['vid'], 'cover'=>$cover, 'link'=>$link, 'addTime'=>$addTime,
                'vtotal'=>$vtotal, 'utotal'=>$utotal,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    /**
     * 我的页面-评论列表2
     */
    public function getMyPlOne($wx,$vid)
    {
        $query = DB::table($this->tPt)->where('del',0)->where('wx',$wx)->where('vid',$vid);
        $total = $query->count();
        $models = $query->skip(0)->take(10)->orderBy('id','desc')->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $addTime = date('Y-m-d H:i',$model['created_at']);
            $items[] = [
                'id'=>$model['id'], 'vid'=>$model['vid'], 'wx'=>$model['wx'],
                'detail'=>$model['detail'], 'addTime'=>$addTime,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    /**
     * 我的页面-分享列表
     */
    public function getMyFxIndex($wx)
    {
        $query = DB::table($this->tFx)->where('del',0)->where('wx',$wx);
        $total = $query->count();
        $models = $query->skip(0)->take(10)->orderBy('id','desc')->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $addTime = date('Y.m.d',$model['created_at']);
            $cover = ''; $link = '';
            $resOne = $this->getOneById($model['vid']);
            if ($resOne['code']==0) {
                $cover = $resOne['data']['vcover'];
                $link =$resOne['data']['vlink'];
            }
            $txtQuery = DB::table($this->tFt)->where('del',0)->where('vid',$model['vid']);
            $vtotal = $txtQuery->count();
            $utotal = $txtQuery->where('wx',$wx)->count();
            $items[] = [
                'vid'=>$model['vid'], 'cover'=>$cover, 'link'=>$link, 'addTime'=>$addTime,
                'vtotal'=>$vtotal, 'utotal'=>$utotal,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    /**
     * 我的页面-分享列表2
     */
    public function getMyFxOne($wx,$vid)
    {
        $query = DB::table($this->tFt)->where('del',0)->where('wx',$wx)->where('vid',$vid);
        $total = $query->count();
        $models = $query->skip(0)->take(10)->orderBy('id','desc')->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $addTime = date('Y-m-d H:i',$model['created_at']);
            $items[] = [
                'id'=>$model['id'], 'addTime'=>$addTime, 'vid'=>$model['vid'], 'wx'=>$model['wx'],
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    /**
     * 农果信息
     */
    public function getOneByTid($tid)
    {
        $models = DB::table($this->tThing)->where('del',0)->where('id',$tid)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $model = $this->jsonToArr($models[0]);
        $cates = DB::table($this->tCate)->where('id',$model['cate'])->get();
        $cateOne = count($cates) ? $this->jsonToArr($cates[0]) : '';
        $cateName = $cateOne ? $cateOne['name'] : '';
        $year = ceil($model['year']/365);
        $out = ceil($model['out']/1000);
        $items = [
            'id'=>$tid, 'cate'=>$cateName, 'name'=>$model['name'], 'serial'=>$model['serial'],
            'year'=>$year, 'out'=>$out, 'desc'=>$model['desc'],
        ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 日常养护
     */
    public function getGuosByTid($tid)
    {
        $models = DB::table($this->tVideo)->where('del',0)//->where('type',4)
            ->where('thing',$tid)->skip(0)->take(2)->orderBy('id','desc')->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            //封面
            $cover = $model['dycover'] ? $model['dycover'] : '';
            if (!$cover) {
                $cover = $model['kscover'] ? $model['kscover'] : '';
            }
            if (!$cover) {
                $cover = $model['cover'] ? $model['cover'] : '';
            }
            //视频链接
            $link = $model['dylink'] ? $model['dylink'] : '';
            if (!$link) {
                $link = $model['kslink'] ? $model['kslink'] : '';
            }
            if (!$link) {
                $link = $model['link'] ? $model['link'] : '';
            }
            $items[] = [
                'vid'=>$model['id'], 'vtitle'=>$model['title'],
                'vcover'=>$cover, 'vlink'=>$link,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 评论
     */
    public function getPlsByTid($tid)
    {
        $models = DB::table($this->tVideo)->where('del',0)//->where('type',4)
            ->where('thing',$tid)->skip(0)->take(100)->orderBy('id','desc')->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $models = $this->jsonToArr($models); $vids = array();
        foreach ($models as $model) {
            $vids[] = $model['id'];
        }
        $model2s = DB::table($this->tPt)->where('del',0)->whereIn('vid',$vids)
            ->skip(0)->take(2)->orderBy('id','desc')->get();
        if (!count($model2s)) { return array('code'=>'-2','msg'=>'no data'); }
        $model2s = $this->jsonToArr($model2s);
        $items = array();
        foreach ($model2s as $model2) {
            $createTime = $model2['created_at'] ? date('Y.m.d',$model2['created_at']) : '-';
            $items[] = [
                'id'=>$model2['id'], 'vid'=>$model2['vid'], 'wx'=>$model2['wx'],
                'detail'=>$model2['detail'], 'createTime'=>$createTime,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 分享
     */
    public function getFxsByTid($tid)
    {
        $models = DB::table($this->tVideo)->where('del',0)//->where('type',4)
            ->where('thing',$tid)->skip(0)->take(100)->orderBy('id','desc')->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $models = $this->jsonToArr($models); $vids = array();
        foreach ($models as $model) {
            $vids[] = $model['id'];
        }
        $model2s = DB::table($this->tFt)->where('del',0)->whereIn('vid',$vids)
            ->skip(0)->take(5)->orderBy('id','desc')->get();
        if (!count($model2s)) { return array('code'=>'-2','msg'=>'no data'); }
        $model2s = $this->jsonToArr($model2s);
        $items = array();
        foreach ($model2s as $model2) {
            $createTime = $model2['created_at'] ? date('Y.m.d',$model2['created_at']) : '-';
            $items[] = [
                'id'=>$model2['id'], 'vid'=>$model2['vid'], 'wx'=>$model2['wx'], 'createTime'=>$createTime,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }
}